// pages/create/create.js


Page({

  /**
   * 页面的初始数据
   */
  data: {
    prices: [{
        "priceName": "免费",
        "select": 0,
        "price": 0,
      },
      {
        "priceName": "1元",
        "select": 1,
        "price": 1,
      },
      {
        "priceName": "2元",
        "select": 2,
        "price": 2,
      },
      {
        "priceName": "3元",
        "select": 3,
        "price": 3,
      },
      {
        "priceName": "4元",
        "select": 4,
        "price": 4,
      },
      {
        "priceName": "5元",
        "select": 5,
        "price": 5,
      },
      {
        "priceName": "8元",
        "select": 6,
        "price": 6,
      },


    ],
    selected: -1, //判断是否选中

    checkboxItems: [{
        index: 0,
        name: '包周（7天）',
        value: "7",
        checked: false
      },
      {
        index: 1,
        name: '包月（30天）',
        value: "30",
        checked: false
      },
      {
        index: 2,
        name: '输入',
        value: "",
        checked: false
      },
    ],

    autoFocusInput: false, //input 是否获取焦点
    word: "", //上个页面输入文字
    currentText: "", //本页面中的文字
    textareaViewHeight: 0,


    imageList: [],
    countIndex: 8,
    count: [1, 2, 3, 4, 5, 6, 7, 8, 9]




  },


  /**
   * 选择定价
   */
  choosePrice: function(event) {
    console.log(event)
    var that = this;
    //data的赋值
    this.setData({
      selected: event.currentTarget.dataset.select
    });
    ////data的取值
    console.log(this.data.prices[event.currentTarget.dataset.select].priceName)
  },

  /**
   * 选择天数
   */
  checkboxChange: function(event) {
    console.log(event)
    var checked = event.currentTarget.dataset.value
    var idx = event.currentTarget.dataset.idx
    console.log(checked)
    var changed = {}
    for (var i = 0; i < this.data.checkboxItems.length; i++) {
      if (idx === i) {
        changed['checkboxItems[' + i + '].checked'] = !this.data.checkboxItems[i].checked
      } else {
        changed['checkboxItems[' + i + '].checked'] = false
      }
    }
    this.setData(changed)

    //键盘获取输入框焦点
    this.setData({
      autoFocusInput: idx === this.data.checkboxItems.length - 1 ? true : false
    })
  },

  /**
   * input天数输入监听
   */
  bindKeyInput: function(input) {
    console.log(input);
  },

  /**
   * input天数获取监听是会触发
   */
  bindFocus: function(input) {
    var that = this
    console.log(input);
    var changed = {}
    for (var i = 0; i < this.data.checkboxItems.length; i++) {
      if (i === this.data.checkboxItems.length - 1) {
        changed['checkboxItems[' + i + '].checked'] = true
      } else {
        changed['checkboxItems[' + i + '].checked'] = false
      }
    }
    this.setData(changed)
    //键盘获取输入框焦点
    this.setData({
      autoFocusInput: true
    })
  },



  /**
   * 跳转到编辑文字页面
   */
  inputWord: function() {
    var word = this.data.word
    wx.navigateTo({
      url: "../../pages/create/word/word?word=" + word
    })
  },

  /**
   * 修改文字监听方法
   */
  bindinput: function(event) {
    this.setData({
      currentText: event.detail.value,
    })
    console.log(this.data.currentText)
  },

  bindlinechange: function(event) {
    console.log(event)
    this.setData({
      //heightRpx
      textareaViewHeight: event.detail.heightRpx,
    })
  },

  /**
   * 清除修改文字
   */
  clearWord: function() {
    //触发回调
    this.setData({
      word: "",
      currentText: "",
    })

  },

  /**
   * 选择图片
   */
  chooseImage: function() {
    var that = this
    wx.chooseImage({
      // sourceType: sourceType[this.data.sourceTypeIndex],
      // sizeType: sizeType[this.data.sizeTypeIndex],
      // count: this.data.count[this.data.countIndex],
      success: function(res) {
        console.log(res)
        var images = res.tempFilePaths;
        for (var i = 0; i < images.length; i++) {
          that.data.imageList.splice(-1, 0, images[i])
        }
        that.setData({
          imageList: that.data.imageList
        })
      }
    })
  },

  /**
   * 预览图片
   */
  previewImage: function(e) {
    console.log(e)
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },

  /**
   * arrayObject.splice(index,howmany,item1,.....,itemX)
   * index 	必需。整数，规定添加/删除项目的位置，使用负数可从数组结尾处规定位置。
  howmany 	必需。要删除的项目数量。如果设置为 0，则不会删除项目。
  item1, ..., itemX 	可选。向数组添加的新项目。
   */
  clearImage: function(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    // var img = 'imageList[' + index + ']'
    var img = this.data.imageList.splice(index, 1)
    console.log(img)
    this.setData({
      imageList: this.data.imageList
    })
  },

  /**
   * 生成二维码
   */
  creatQrcode: function() {
    var word = this.data.word
    wx.navigateTo({
      url: "../../pages/create/rqCode/rqCode",
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //获得自定义组件


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(this.data.word);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})